//
//  MyOrderListViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/3/28.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MyOrderListViewController.h"
#import "OrdersTableViewCell.h"
#import "SDAutoLayout.h"
#import "UIView+Extension.h"
#import "OrderDetailViewController.h"
#import "AllOrderViewController.h"
#import "CancleVC.h"
#import "LogisticsViewController.h"
#import "OrderDetailModel.h"
#import "PayViewController.h"
#import "AfterSalesTableViewCell.h"
#import "SalesDetailViewController.h"
#import "SalesChangeViewController.h"
#import "SalesReplenishmentViewController.h"
@interface MyOrderListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) UIView * alpheView;
@property (nonatomic, strong) OrderDetailModel *orderModle;
@property (nonatomic ,strong) NSMutableArray *orderArray;
@property (nonatomic ,strong) UIView *blankView;
@property (nonatomic, assign) int  pageNo;

@end

@implementation MyOrderListViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.orderArray removeAllObjects];
    self.pageNo=1;
    [self loadaftersalesData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"售后");
    [self createdUI];
    self.orderArray=[[NSMutableArray alloc]init];
    self.blankView=[ZJUITools showBlankView];
    self.blankView.hidden=YES;
    [self.view addSubview:self.blankView];
    
}

- (void)createdUI
{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH-64) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    tableView.backgroundColor=Gray_Color;
    tableView.separatorStyle=0;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:tableView];
    [tableView registerNib:[UINib nibWithNibName:@"AfterSalesTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AfterSalesTableViewCell"];
    tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageNo=1;
        [self.orderArray removeAllObjects];
        [self loadaftersalesData];
    }];
    tableView.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.pageNo++;
        [self loadaftersalesData];
        [tableView.mj_footer endRefreshing];
    }];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.orderArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
       return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 208;
}
#pragma mark-------订单里的商品信息
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.orderModle=self.orderArray[indexPath.section];
    AfterSalesTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AfterSalesTableViewCell"];
    cell.orderLabel.text=[NSString stringWithFormat:@"%@:  %@",MyString(@"受理单号"),self.orderModle.aftersalesId];
    NSString *time=[self timeWithTimeIntervalString:self.orderModle.createdTime];
    //cell.timeLabel.text=[NSString stringWithFormat:@"申请时间:%@",time];
    cell.timeLabel.text=[NSString stringWithFormat:@"%@:  %@",MyString(@"申请时间"),time];
    [cell.shopImag sd_setImageWithURL:[NSURL URLWithString:self.orderModle.imageUrl]];
    cell.goodsName.text=self.orderModle.spuName;
    cell.shopType.text=[NSString stringWithFormat:@"工艺类型:%@",self.orderModle.saleName];
    cell.shopNum.text=[NSString stringWithFormat:@"%@: ×%@",MyString(@"数量"),self.orderModle.quantity];
    cell.moneyLabel.text=[NSString stringWithFormat:@"%@: ¥ %@",MyString(@"退款金额"),self.orderModle.refund];
    cell.detailBtn.tag=indexPath.section;
    [cell.detailBtn addTarget:self action:@selector(lookDetail:) forControlEvents:UIControlEventTouchUpInside];
    [cell.conactBtn addTarget:self action:@selector(conactBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    if ([self.orderModle.aftersalesType isEqualToString:@"tui"]) {
        cell.moneyLabel.hidden=NO;
        cell.iconImg.image=[UIImage imageNamed:@"icon_return"];
    }else if ([self.orderModle.aftersalesType isEqualToString:@"huan"]){
        cell.iconImg.image=[UIImage imageNamed:@"icon_exchange"];
        cell.moneyLabel.hidden=YES;
    }else{
        cell.iconImg.image=[UIImage imageNamed:@"icon_replenish"];
        cell.moneyLabel.hidden=YES;
    }
    switch ([self.orderModle.aftersalesStatus intValue]) {
        case 0:
        {
          cell.statueLabel.text=MyString(@"已撤销");
        }
            break;
            
        case 1:
        {
            cell.statueLabel.text=MyString(@"已拒绝");
        }
            break;
            
        case 2:
        {
            cell.statueLabel.text=MyString(@"待审核");
        }
            break;
            
        case 3:
        {
            cell.statueLabel.text=MyString(@"已同意");
        }
            break;
            
        case 4:
        {
            cell.statueLabel.text=MyString(@"待卖家确认");
        }
            break;
            
        case 5:
        {
            cell.statueLabel.text=MyString(@"待卖家确认");
        }
            break;
            
        case 6:
        {
            cell.statueLabel.text=MyString(@"待买家确认");
        }
            break;
        case 7:
        {
            cell.statueLabel.text=MyString(@"已完成");
        }
            break;
            
        default:
            break;
    }
    CGSize size=[cell.statueLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    cell.statueLabel.frame=CGRectMake(MainW-size.width-10, 19, size.width, 18);
    cell.iconImg.frame=CGRectMake(MainW-size.width-33, 19, 18, 18);
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    
}
-(void)loadaftersalesData{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"pageNumber"]=@(self.pageNo);
    dic[@"pageSize"]=@(10);
    [self doHttpRequestWithApi:API_APPLYSALESPAGE method:GET dic:dic successBlock:^(NSDictionary *dic) {
        [self.orderArray addObjectsFromArray:[OrderDetailModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"list"]]];
        if (self.orderArray.count==0) {
            self.blankView.hidden=NO;
        }else{
            self.blankView.hidden=YES;
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }
    }];
}
-(void)lookDetail:(UIButton *)sender{
     self.orderModle=self.orderArray[sender.tag];
    if ([self.orderModle.aftersalesType isEqualToString:@"tui"]) {
        SalesDetailViewController*vc=[[SalesDetailViewController alloc]init];
        vc.aftersalesId=self.orderModle.aftersalesId;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([self.orderModle.aftersalesType isEqualToString:@"huan"]){
        SalesChangeViewController*vc=[[SalesChangeViewController alloc]init];
        vc.aftersalesId=self.orderModle.aftersalesId;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        SalesReplenishmentViewController*vc=[[SalesReplenishmentViewController alloc]init];
        vc.aftersalesId=self.orderModle.aftersalesId;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
-(void)conactBtnClick:(UIButton *)sender{
     self.orderModle=self.orderArray[sender.tag];
    EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:[NSString stringWithFormat:@"%@",self.orderModle.sellerIm] conversationType:EMConversationTypeChat];
    chatController.title=[NSString stringWithFormat:@"%@",self.orderModle.sellerIm];
    [self.navigationController pushViewController:chatController animated:YES];
    
}
@end
